package com.photo.photePro.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photo.photePro.common.R;
import com.photo.photePro.entity.Like;
import com.photo.photePro.service.LikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 点赞Controller
 */
@Slf4j
@RestController
@RequestMapping("/likes")
public class LikeController {

    @Autowired
    private LikeService likeService;

    /**
     * 分页查询用户点赞
     */
    @GetMapping("/user")
    public R<Page<Like>> pageUserLikes(int page, int pageSize, Long userId) {
        log.info("分页查询用户点赞: page={}, pageSize={}, userId={}", page, pageSize, userId);
        
        if (userId == null) {
            return R.error("用户ID不能为空");
        }
        
        Page<Like> pageInfo = likeService.pageUserLikes(page, pageSize, userId);
        return R.success(pageInfo);
    }
    
    /**
     * 添加点赞
     */
    @PostMapping
    public R<String> addLike(@RequestBody Map<String, Long> params) {
        log.info("添加点赞: {}", params);
        
        Long photoId = params.get("photoId");
        Long userId = params.get("userId");
        
        if (photoId == null || userId == null) {
            return R.error("参数错误");
        }
        
        boolean result = likeService.addLike(photoId, userId);
        if (result) {
            return R.success("点赞成功");
        } else {
            return R.error("点赞失败");
        }
    }
    
    /**
     * 取消点赞
     */
    @DeleteMapping("/{id}")
    public R<String> removeLike(@PathVariable Long id) {
        log.info("取消点赞: id={}", id);
        
        Like like = likeService.getById(id);
        if (like == null) {
            return R.error("点赞记录不存在");
        }
        
        boolean result = likeService.removeById(id);
        if (result) {
            return R.success("取消点赞成功");
        } else {
            return R.error("取消点赞失败");
        }
    }
    
    /**
     * 检查用户是否已点赞该作品
     */
    @GetMapping("/check")
    public R<Map<String, Boolean>> checkIsLiked(Long photoId, Long userId) {
        log.info("检查是否已点赞: photoId={}, userId={}", photoId, userId);
        
        if (photoId == null || userId == null) {
            return R.error("参数错误");
        }
        
        boolean isLiked = likeService.isLiked(photoId, userId);
        Map<String, Boolean> result = new HashMap<>();
        result.put("isLiked", isLiked);
        
        return R.success(result);
    }
} 